package com.web;


import com.entity.User;
import com.service.UserService;
import com.utils.EncryptCodeUtils;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
@MultipartConfig
public class LoginServlet extends HttpServlet {

    private UserService userService = new UserService();

    //request对象
    private HttpServletRequest request;
    //response对象
    private HttpServletResponse response;

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //初始化请求和响应
        this.request = request;
        this.response = response;
        String method = request.getParameter("method");
        switch (method) {
            case "login": login(); break;
            case "logOut": logOut(); break;
            case "toLoginPage": toLoginPage(); break;
            case "toRegPage": toRegPage(); break;
            case "reg": regUser(); break;
            case "checkUsername": checkUsername(); break;
            case "index": showIndex(); break;
        }
    }

    /**
     * 跳转到登陆界面
     */
    private void toLoginPage() throws ServletException, IOException {
        WebUtils.toRequestPage(request, response, "login");
    }

    /**
     * 跳转到注册页面
     */
    private void toRegPage() throws ServletException, IOException {
        WebUtils.toRequestPage(request, response, "reg");
    }

    /**
     * 登出
     */
    private void logOut() throws IOException {
        //获取当前的session
        HttpSession session = request.getSession();
        //session.invalidate();//清空，销毁session
        session.removeAttribute("user");
        session.setAttribute("msg","注销成功，请重新登录");
        response.sendRedirect("login?method=toLoginPage");
    }

    private void showIndex() {

    }

    /**
     * 核对用户名是否已存在
     */
    private void checkUsername() {
        String username = request.getParameter("username");
        User user = userService.queryUserByUsername(username);
        WebUtils.sendMSG(response, (user==null)?"1":"-1");
    }

    /**
     * 注册用户
     */
    private void regUser() throws IOException, ServletException {
        //获取当前的session
        HttpSession session = request.getSession();
        //正式注册
        User user = WebUtils.getBean(User.class, request);

        //设置盐值
        user.setPhone(WebUtils.getRandomString());
        //重新设置密码
        user.setPassword(EncryptCodeUtils.getEncryptStr(user.getPassword(),
                user.getPhone()));

        user.setAdministrators("F");
        user.setSupertube("F");
        //重新设置文件路径
        //user.setPic(WebUtils.uploadFile(request, "pic"));
        user.setPic(null);
        //插入数据
        boolean result = userService.addUser(user);

        //增加完成之后，跳转到首页
        session.setAttribute("msg",result?"注册成功":"注册失败");
        session.setAttribute("result", result);
        //跳转登陆页面
        response.sendRedirect("login?method=toLoginPage");
    }

    /**
     * 用户登陆
     */
    private void login() throws IOException {
        //获取当前的session
        HttpSession session = request.getSession();
        //前端传递过来的用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //通过用户名查询数据库的数据
        User user = userService.queryUserByUsername(username);
        String msg = "";
        if (user == null) {
            msg = "用户不存在！！！";
        } else {
            //先把传递过来的密码加密一遍
            String encryptPwd = EncryptCodeUtils.getEncryptStr(password, user.getPhone());
            if (user.getPassword().equals(encryptPwd)) {
                msg = "登陆成功";
//                session.setAttribute("msg", msg);
//                session.setAttribute("flag", true);
                session.setAttribute("user", user);
                //多长时间没有操作页面就T出去
                //session.setMaxInactiveInterval(3*60);
                //清空
                //session.invalidate();
                //移除
                //session.removeAttribute("house");
                //直接到首页
                response.sendRedirect("user?method=toIndex");
                return;
            } else {
                msg = "密码错误！！！";
            }
        }
        //返回错误信息
        session.setAttribute("msg", msg);
        session.setAttribute("result", false);

        //直接到登陆界面
        response.sendRedirect("login?method=toLoginPage");
    }
}
